/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Jan 10, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.gui;

import edu.psu.geovista.app.pcp.plot.control.Settings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *  
 *  
 */
public  class PcpCanvas extends JPanel
                        implements Scrollable{//
    public static final boolean DEBUG=false;
    private boolean paintLayer=true;//for debug purpose, if false, no layer will be painted

    private BasicPcpUI pcpUI;

    JScrollPane containerScrollPane;

    private static final Logger logger=Logger.getLogger(PcpCanvas.class.getName() );
    static {
        //set up logger
        Level mylevel= Level.INFO;
          ConsoleHandler handler=new ConsoleHandler();
          logger.addHandler(handler);  // 

          logger.setLevel(mylevel);
          handler.setLevel(mylevel );
    }

    public PcpCanvas(BasicPcpUI basicPcpUI) {
        this.pcpUI = basicPcpUI;

        Settings settings = pcpUI.getSettings();
        Color bgcolor = (Color) settings.getSetting(Settings.APPR_BACKGROUND);

        this.setBackground( bgcolor);
        this.setForeground(bgcolor );

       /* this.legend =new LegendControl(LegendControl.getSampleColors() );
        legendLayoutMgr = new LegendLayoutor(this,legend);//
        legendLayoutMgr.setContaineeLocType(ContaineeLocator.TOP_RIGHT);
        legendLayoutMgr.resetContaineeLocation();
        ContaineeDragger drag = new ContaineeDragger(legend);
        drag.addContaineeDraggedListener(new PropertyChangeListener(){
            public void propertyChange(PropertyChangeEvent evt) {
                Point loc = (Point) evt.getNewValue();// 
                legendLayoutMgr.setCustomizeLocation(loc,ContaineeLocator.CUSTOMIZE_REL);
            }
        });*/



    }

    public void setContainerScrollPane(JScrollPane containerScrollPane) {
        this.containerScrollPane = containerScrollPane;

    }



    /*******************************************************************************************************
     *                support
     *******************************************************************************************************/

    /*public void repaint(){

        super.repaint();
        pcpCanvasRepainted();

    }*/
    /**
     *  
     * @param g
     */
    public void paintComponent(Graphics g){
        paintBackground(g);
        logger.fine("");
        /**/

    }

    protected void paintBackground(Graphics g) {
        int w=this.getSize().width ;
        int h=this.getSize().height ;
        Color c=g.getColor() ;

        Color bgcolor = this.getBackground();
        g.setColor(bgcolor );
        g.fillRect(0,0,w,h);

        if (paintLayer){
             pcpUI.paintLayers(g,((JComponent)pcpUI.getPlot()));
        }
        else{ //only for development purpose to turn off paintlayer to save time
            System.out.println("paint layer turn off");
        }

        g.setColor(c);
    }

    public boolean contain(JComponent c){
        Component[] comps = this.getComponents();
        if(comps==null)return false;
        for (int i=0;i<comps.length ;i++){
            if(comps[i].equals(c) )return true;
        }
        return false;
    }
    /*******************************************************************************************************
     *                scrollbar
     *******************************************************************************************************/
    // 
    public Dimension getPreferredScrollableViewportSize() {
      // return plot.getPlotSize() ; //To keep the scrollPane size=viewport(PcpCanvas) size

      //  return plot.getSize() ;
       return new Dimension(900,400);
    }
    // 
    public Dimension getPreferredSize()  {
       return pcpUI.plot.getPlotSize() ;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 5;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 5;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false; // 
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    /**************************************************
     *                mouse events
     ***************************************************/
    public void forwardMouseEvent(MouseEvent e) {
        this.processMouseEvent(e);
    }
    public void forwardMouseMotionEvent(MouseEvent e) {
        this.processMouseMotionEvent(e);
    }





}
